﻿Ext.define("PSI.User.ChangeMyPasswordForm", {
    createUI: function (user) {
        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            id:"zp",
            layout: "border",
            title: "修改我的登录密码",
            items: [
                {
                    region:"north",
                    xtypr: "panel",
                    layout: {
                        type: 'hbox',
                        pack: 'center',
                        align: 'middle'
                    },
                    border: 0,
                    height: "30%",
                    items: [
                       {
                           xtype: 'label',
                           id:'alert',
                           forId: 'myFieldId',
                           text: '提示:检测到您的密码为默认密码，请先修改',
                           style: {
                               fontSize:"30px",
                               color: "red"
                           }
                       }
                    ]
                }
                ,{
                    region: "center",
                    border: 0,
                    xtype: "panel",
                    layout: {
                        type: 'hbox',
                        pack: 'center',
                        align: 'top'
                    },
                    items: [
                          {
                            id: "editForm",
                            xtype: "form",
                            layout: "anchor",
                            title:'修改密码',
                            height:230,
                            width: 370,
                            defaultType: 'textfield',
                            fieldDefaults: {
                                labelWidth: 60,
                                labelAlign: "right",
                                labelSeparator: "",
                                msgTarget: 'side'
                            },
                            items: [
                                {
                                    xtype: "hidden",
                                    name: "id",
                                    value: user.id
                                }, {
                                    xtype: 'tbspacer',          //插入的空填充
                                    anchor: "100% 10%"
                                },
                                {
                                    fieldLabel: "登录名:",
                                    xtype: "displayfield",
                                    value: user.loginName,
                                    anchor:"90% 15%"
                                },
                                {
                                    fieldLabel: "姓名:",
                                    xtype: "displayfield",
                                    value: user.name,
                                    anchor:"90% 15%"
                                },
                                {
                                    id: "editOldPassword",
                                    fieldLabel: "旧密码:",
                                    allowBlank: false,
                                    blankText: "没有输入旧密码",
                                    beforeLabelTextTpl: PSI.Const.REQUIRED,
                                    inputType: "password",
                                    name: "oldPassword",
                                    anchor: "90% 15%"
                                 
                                },
                                {
                                    id: "editNewPassword",
                                    fieldLabel: "新密码:",
                                    allowBlank: false,
                                    blankText: "没有输入新密码",
                                    beforeLabelTextTpl: PSI.Const.REQUIRED,
                                    inputType: "password",
                                    name: "newPassword",
                                    anchor: "90% 15%"
                                },
                                {
                                    id: "editConfirmPassword",
                                    fieldLabel: "确认密码:",
                                    allowBlank: false,
                                    blankText: "没有输入确认密码",
                                    beforeLabelTextTpl: PSI.Const.REQUIRED,
                                    inputType: "password",
                                    anchor: "90% 15%"
                                },
                            ],
                            buttons: [
                                { text: "修改密码", formBind: true, handler: this.onOK, scope: this, iconCls: "psi-button-ok" },
                                {
                                    text: "取消", handler: function () {
                                        location.replace("/");
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        });

        this.panel = panel;
    },

    getMainPanel: function () {
        return this.panel;
    },

    getAlertEL:function(){
        //  var cc = Ext.getCmp("alert");
        var me = this;
        var f = this.panel;
        Ext.getCmp("alert").setVisible(false);
    },

    getOldPasswordEL:function(){
        return Ext.getCmp("editOldPassword");
    },

    onOK: function () {
        var editNewPassword = Ext.getCmp("editNewPassword");
        var editConfirmPassword = Ext.getCmp("editConfirmPassword");

        var np = editNewPassword.getValue();
        var cp = editConfirmPassword.getValue();

        if (np != cp) {
            PSI.MsgBox.showInfo("确认密码与新密码不一致", function () {
                editNewPassword.focus();
            });
            return;
        }

        var form = Ext.getCmp("editForm");
        var el = Ext.getBody();
        form.submit({
            url: "/User/ChangeMyPasswordPOST",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo("成功修改登录密码", function () {
                    location.replace("/Home/Index");
                });
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg);
            }
        });
    }
});